﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
namespace PerceptiveMCAPI.Types
{
   public class listsForEmailOutput : Api_BaseOutput
   {
      public List<string> result { get; set; }
      public listsForEmailParms inputParms { get; private set; }
      //
      public listsForEmailOutput( listsForEmailInput input )
         : base( input )
      {
         this.result = new List<string>();
         this.inputParms = input.parms;
      }
      // ----------------------------------
      public override string ToString()
      {
         return string.Format( "Lists: {0}", apiHelper.MakeStringFromList( result ) );
      }
      #region ================================================================== format setup
      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }
      // --------------------------------------------------------------------
      // ---- xmlrpc
      internal void Format( string[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = apiHelper.MakeListFromArray( rpcResult );
      }
      #endregion

      #region ================================================================= Output
      // ---------------------------------------------------------- outputXML
      protected void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            IEnumerable<XElement> list_array = xml.Elements( "struct" );
            foreach ( XElement list_id in list_array )
            {
               this.result.Add( list_id.Value );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // ---------------------------------------------------------- outputJSON
      protected void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();
         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               this.result = jss.Deserialize<List<string>>( this.api_Response );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // --------
      #endregion
   }
}
